package com.urban.pooling.controller.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.urban.pooling.domain.Route;
import com.urban.pooling.domain.RouteRequest;
import com.urban.pooling.domain.RouteRequestStatus;
import com.urban.pooling.domain.User;
import com.urban.pooling.dto.RouteRequestDto;
import com.urban.pooling.service.RouteRequestService;
import com.urban.pooling.service.RouteService;
import com.urban.pooling.service.UserService;
import com.urban.pooling.util.AppUtil;
import com.urban.pooling.util.DomainUtil;

@Controller
@RequestMapping("/api/routerequests")
public class RouteRequestApiController {

	@Autowired
	RouteRequestService routeReqService;

	@Autowired
	UserService userService;

	@Autowired
	RouteService routeService;

	private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

	@RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
	public @ResponseBody
	String getRouteRequests(@RequestParam(value = "status") String status) {
		User user = userService.findByUsername(AppUtil.getAuthenthicatedUsername());
		Iterable<RouteRequest> routeRequests = routeReqService.find(user, status);
		List<RouteRequestDto> dtos = new ArrayList<RouteRequestDto>();
		for(RouteRequest routeRequest: routeRequests){
			dtos.add(DomainUtil.createDto(routeRequest));
		}
		return gson.toJson(dtos);
	}

	@RequestMapping(value = { "", "/" }, method = RequestMethod.POST)
	public ResponseEntity<String> addRouteRequest(@RequestBody String routeRequestBody) {
		RouteRequestDto routeRequestDto = gson.fromJson(routeRequestBody, RouteRequestDto.class);

		Route route = routeService.findOne(routeRequestDto.getRouteId());
		RouteRequestStatus pendingStatus = routeReqService.getRequestStatus(RouteRequestStatus.PENDING);
		User createdBy = userService.findByUsername(AppUtil.getAuthenthicatedUsername());
		RouteRequest request = DomainUtil.createRouteRequest(routeRequestDto, createdBy, route, pendingStatus);

		try {
			routeReqService.save(request);
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return new ResponseEntity<String>("{}", HttpStatus.OK);
	}

	@RequestMapping(value = "/{routeRequestId}", method = RequestMethod.PUT)
	public ResponseEntity<String> updateRouteRequest(@PathVariable("routeRequestId") Integer routeRequestId,
			@RequestBody String routeRequestBody) {
		RouteRequestDto routeRequestDto = gson.fromJson(routeRequestBody, RouteRequestDto.class);
		routeReqService.updateStatus(routeRequestDto.getId(), routeRequestDto.getStatus());
		return new ResponseEntity<String>("{}", HttpStatus.OK);
	}
}